var status=false;
var goodsId=$("#goodsId").val();
var buyNum=$("#buyNum").val();
var spec=$("#spec").val();
var type=$("#type").val();
$(function(){
	if($("#address").val()==""){
		$("#addressData").html('请填写您的所在区域 ');
	}else{
		$("#addressData").html($("#address").val());
	}
});
if($("#status").val()=='1'){
	$("#checkStatus").addClass("simulation_checkbox_active");
	status=true;
}
if(type!=""){
	$("#submitClick").text("保存并使用");
}else{
	$("#submitClick").text("提交");
}
// 默认地址
form.Checkbox('.data_checkbox', {
  onchange: function (Checkbox) {
	  status = Checkbox.iSelect;
  }
})

function addSave(){
	var id=$("#addressID").val();
	var contextPath=$("#contextPath").val();
	var deliveryName=$("#deliveryName").val();
	var deliveryPhone=$("#deliveryPhone").val();
	var cityIDList=$("#cityIDList").val();
	var cityid=$("#cityid").val();
	var cityName=$("#cityName").val();
	var addressDetail=$("#addressDetail").val();
	if(status=="false"){
		status='0';
	}else{
		status='1';
	}
	if(deliveryPhone.length!=11){
		 layer.open({
			    content:"请输入有效的手机号码！",
			    time: 10000
			});
		return;
	}
	
	if(id!=null && id!=""){
		if(cityIDList==null || cityIDList==""){
			layer.open({
			    content:"请输入住址信息！",
			    time: 10000
			});
			return;
		}
	}else{
		if(cityid==null || cityid==""){
			layer.open({
			    content:"请输入住址信息！",
			    time: 10000
			});
			return;
		}
	}
	
	if(deliveryPhone==null || deliveryPhone==""){
		layer.open({
		    content:"请输入手机号码！",
		    time: 10000
		});
		return;
	}
	if(deliveryName==null || deliveryName==""){
		layer.open({
		    content:"请输入姓名！",
		    time: 10000
		});
		return;
	}
	if(addressDetail==null || addressDetail==""){
		layer.open({
		    content:"请输入详细地址！",
		    time: 10000
		});	
		return;
	}
	$.ajax({
		url:contextPath +"/address/addSave",
		type:"post",
		async:true,
		dataType:"json",
		data:{"params['addressID']":id,"params['cityid']":cityid,"params['cityName']":cityName,"params['deliveryName']":deliveryName,"params['deliveryPhone']":deliveryPhone,"params['addressDetail']":addressDetail,"params['cityIDList']":cityIDList,"params['status']":status,"params['goodsId']":goodsId},
		success : function(data, textStatus) {
			var state = data.result.state;
			var addressId=data.addressId;
			$("#addressId").val(addressId);
			if(state==1){
				layer.open({
				    content:data.result.message,
				    time: 10000
				});
				if(type=="confirm_shopCart"){
					$("#addressForm").attr("action",contextPath+"/pay/confirm_shopCart").submit();
				}else if(type=="buy_now"){
					$("#addressForm").attr("action",contextPath+"/pay/buy_now").submit();
				}else{
					window.location.href=contextPath+"/address";
				}
				
			}else{
				layer.open({
				    content:data.result.message,
				    time: 10000
				});
			}
		},
		error : function(XMLHttpRequest, textStatus, errorThrown){
			layer.open({
			    content:textStatus,
			    time: 10000
			});
		}
	});
	
}